#!/usr/bin/env python
#coding=utf-8

from phprpc import PHPRPC_WSGIApplication, UrlMapMiddleware
from beaker.middleware import SessionMiddleware
from google.appengine.ext.webapp.util import run_wsgi_app
import phpformat, hashlib
from sso.response_code import *
SSO_ENC_KEY = 'bawbaw_icittys_com_key!!'

def login(sso_info,session):
    serialize_user = sso_info['info']
    time = sso_info['time']
    sign = sso_info['sign']
    verify_sign = hashlib.md5(SSO_ENC_KEY + serialize_user + time).hexdigest()
    if sign != verify_sign:return HTTP_RESPONSE_SIGN_NO
    session['user'] = phpformat.unserialize(serialize_user)
    return HTTP_RESPONSE_LOGIN_OK

def logout(session):
    del session['user']
    return HTTP_RESPONSE_LOGOUT_OK

server = PHPRPC_WSGIApplication(sessionName = 'beaker.session')
server.add(login)
server.add(logout)
server.debug = True
server = UrlMapMiddleware([('/rpc_member', server)])
server = SessionMiddleware(server, type='ext:google', table_name="beaker_session", cookie_expires=False)
run_wsgi_app(server)
